package com.amoby.main.action.order;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amoby.base.common.BaseAction;
import com.amoby.main.service.PushSmsService;

/**
 * <p>Title：PushSmsAction</p>
 * <p>Desc：</p>
 * @address http://www.yingmob.com/
 * @author zhaoyy
 * @date 2016-2-26 上午10:19:53
 */
@Controller
public class PushSmsAction extends BaseAction {
    private Log               logger = LogFactory.getLog(getClass());
    @Autowired
    private PushSmsService pushSmsService;
    
    /**
     * 短信通知回调接口
     * @author Lasing 2016-2-26 上午10:21:03
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/sms/smsnotifyintface")
    public Map<String, Object> smsnotifyintface(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            String orderId     = getRequestParams(String.class, request, "orderId");  //订单号/转账id
            String phone       = getRequestParams(String.class, request, "phone");    //手机号
            int    type        = getRequestParams(Integer.class, request, "type");    //短信类型
            String pushTime    = request.getParameter("pushTime");                    //推送接收时间
            String sendTime    = request.getParameter("sendTime");                    //短信发送时间
            String successTime = request.getParameter("successTime");                 //短信成功时间
            int    status      = getRequestParams(Integer.class, request, "status");  //短信发送状态
            logger.debug("短信回调来了======="+"orderId="+orderId+"=====&phone="+phone+"=====&type="+type+"=====&pushTime="+pushTime
                    +"=====&sendTime="+sendTime+"=====&successTime="+successTime+"=====&status="+status);
            if(orderId!=null && !"".equals(orderId)){ //如果订单号为空则不处理
                if(pushTime!=null &&!"".equals(pushTime)){  //如果推送接收时间不为空  即推送成功且收到推送了
                    pushSmsService.updPushSmsLogTimeAndStatus(orderId,type,pushTime);
                }
                if(sendTime!=null &&!"".equals(sendTime)){  //短信发送时间
                    pushSmsService.addSendSms(orderId,phone,type,sendTime,status);
                }
                if(successTime!=null &&!"".equals(successTime)){  //短信发送成功时间
                    pushSmsService.updSendSms(orderId,phone,type,successTime);
                }
            }
            result.put("result", "success");
            result.put("msg", "短信回调成功！");
        } catch (Exception e) {
            result.put("result", "fail");
            result.put("msg", "短信回调失败！");
            logger.error("短信通知回调错误",e);
        }
        return result;
    }
    
    public static void main(String[] args) {
        
        String[] array2 = { "8615215020424", "15625104360", "18716664540", "222222" };
        String[] array1 = { "8615215020424", "15625104360", "18716664540" };
        Arrays.sort(array2);
        Arrays.sort(array1);
        
        for (String str : array2) {
            if (Arrays.binarySearch(array1, str) < 0) {
                System.out.println("第二个数组中没有第一个数组的元素:" + str);
            }
        }
        for (String str : array1) {
            if (Arrays.binarySearch(array2, str) < 0) {
                System.out.println("第一个数组中没有第二个数组的元素:" + str);
            }
        }
    }
}
